<template>
  <div class="login-page">
    <div class="container">
      <h2>用户登录</h2>
      <el-form :model="loginForm" ref="loginForm" :rules="rules">
        <el-form-item prop="email">
          <el-input 
            v-model="loginForm.email" 
            placeholder="请输入邮箱"
            prefix-icon="Message"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input 
            v-model="loginForm.password" 
            type="password" 
            placeholder="请输入密码"
            prefix-icon="Lock"
            show-password
          ></el-input>
        </el-form-item>
        <el-button type="primary" class="login-btn" @click="handleLogin" :loading="loading">
          登录
        </el-button>
        <div class="links">
          <el-button link @click="goToRegister">注册账号</el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue';
import { useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';
import { useAuthStore } from '../store/authStore';

const router = useRouter();
const authStore = useAuthStore();
const loading = ref(false);

const loginForm = reactive({
  email: '',
  password: ''
});

const rules = {
  email: [
    { required: true, message: '请输入邮箱', trigger: 'blur' },
    { type: 'email', message: '请输入正确的邮箱格式', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能小于6位', trigger: 'blur' }
  ]
};

const handleLogin = async () => {
  loading.value = true;
  try {
    // 使用 admin/admin 作为测试账号
    if (loginForm.email === 'admin@admin.com' && loginForm.password === 'admin123') {
      ElMessage.success('登录成功');
      authStore.setToken('test-token');
      router.push('/register');
      return;
    }

    // 正常的API登录逻辑
    const response = await fetch('https://penglink.pcwxcn.top/api/admin/login', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({
        name: loginForm.email,
        password: loginForm.password
      })
    });

    const data = await response.json();
    if (data.code === 200) {
      ElMessage.success('登录成功');
      authStore.setToken(data.data.token);
      router.push('/register');
    } else {
      ElMessage.error(data.msg || '登录失败，请检查账号密码');
    }
  } catch (error) {
    ElMessage.error('登录失败：' + error.message);
  } finally {
    loading.value = false;
  }
};

const goToRegister = () => {
  router.push('/register');
};
</script>

<style scoped>
.login-page {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #f5f5f5;
}

.container {
  width: 400px;
  padding: 30px;
  background-color: white;
  border-radius: 8px;
  box-shadow: 0 0 10px rgba(0,0,0,0.1);
}

h2 {
  text-align: center;
  color: #333;
  margin-bottom: 30px;
}

.login-btn {
  width: 100%;
  margin-top: 20px;
}

.links {
  text-align: center;
  margin-top: 20px;
}

:deep(.el-input__wrapper) {
  background-color: #f5f5f5;
}

:deep(.el-button--text) {
  padding: 0;
}
</style>